package action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CodeAction  extends ActionSupport implements SessionAware,ServletResponseAware   {
	
	 private Map<String, Object> session;  
	    private HttpServletResponse response;  
	    private static final long serialVersionUID = 1L;  
	    @Override  
	    public String execute() throws Exception  {  
	    	BufferedImage bi = new BufferedImage(68,22,BufferedImage.TYPE_INT_RGB);
			Graphics g = bi.getGraphics();
			Color c = new Color(200,150,255);
			g.setColor(c);
			g.fillRect(0, 0, 68, 22);
			
			char[] ch = "ABCDEFGHJKMNOPQRSTUVWXYZabcdefghjkmnopqrstuvwxyz0123456789".toCharArray();
			Random r = new Random();
			int len=ch.length,index;
			StringBuffer sb = new StringBuffer();
			for(int i=0; i<4; i++){
				index = r.nextInt(len);
				g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));
				g.drawString(ch[index]+"", (i*15)+3, 18);
				sb.append(ch[index]);
			}
			session.put("piccode", sb.toString());
			ImageIO.write(bi, "JPG", response.getOutputStream());
			return null;
	    }  
	 
	 
	    @Override  
	    public void setSession(Map<String, Object> session)  
	    {  
	        // TODO Auto-generated method stub  
	        this.session = session;  
	    }  
	    @Override  
	    public void setServletResponse(HttpServletResponse response)  
	    {  
	        // TODO Auto-generated method stub  
	        this.response = response;  
	    }  
	   
	

}
